package org.example.controller;

import org.example.entity.Order;
import org.example.service.OrderService;
import org.example.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 获取所有订单
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllOrders() {
        return ResponseEntity.ok(ResponseUtil.success("Orders fetched successfully!", orderService.getAllOrders()));
    }

    // 根据ID获取订单信息
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getOrderById(@PathVariable Long id) {
        Order order = orderService.getOrderById(id);
        if (order != null) {
            return ResponseEntity.ok(ResponseUtil.success("Order fetched successfully!", order));
        } else {
            return ResponseEntity.status(404).body(ResponseUtil.error(404, "Order not found"));
        }
    }

    // 创建订单
    @PostMapping
    public ResponseEntity<Map<String, Object>> createOrder(@RequestBody Order order) {
        orderService.createOrder(order);
        return ResponseEntity.ok(ResponseUtil.success("Order created successfully!"));
    }

    // 更新订单状态
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateOrder(@PathVariable Long id, @RequestBody Order order) {
        orderService.updateOrder(id, order);
        return ResponseEntity.ok(ResponseUtil.success("Order updated successfully!"));
    }

    // 删除订单
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteOrder(@PathVariable Long id) {
        orderService.deleteOrder(id);
        return ResponseEntity.ok(ResponseUtil.success("Order deleted successfully!"));
    }
}
